//
// Copyright © 2021 dev. All rights reserved.
// 

import Cocoa
import ConfCore
import Realm
import os.log
@objc final class WWDCAgentService:NSObject {
    let log = OSLog.agentLog(with:String(describing: WWDCAgentService.self))

    private var storage:Storage?
    private var syncEngine:SyncEngine?
    private let boot = Boot()

    lazy var listener:NSXPCListener = {
        let l = NSXPCListener(machServiceName: "io.wwdc.app.WWDCAgent")
        l.delegate = self
        return l
    }()

    func start() {
        boot.bootstrapDependencies(readOnly: false) { [weak self] result in
            guard let self = self else { return}
            switch result {
            case .success(let dependencies):
                self.storage = dependencies.storage
                self.syncEngine = dependencies.syncEngine
                os_log("Bootstrapped successfully!",log:self.log,type:.default)

            }
        }
    }
    private func startListening()  {
        dispatchPrecondition(condition: .onQueue(.main))
        listener.resume()
        os_log("XPC service started",log:self.log,type:.default)
    }

}

extension WWDCAgentService:NSXPCListenerDelegate {
    func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
        os_log("%{public}@ %{public}@",log:log, type:.debug,#function,newConnection)
//        guard XPCConne else {
//            <#statements#>
//        }
    }
}
